#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
# Date: 2018/10/29

import numpy as np
from datetime import datetime, timedelta
import pandas as pd

from weater_rule_01 import Expression


############出行温馨提示基本参数###############
class Rule(object):
    def __init__(self,set_weater,original_data,set_time,end_time,code_name_set,code_name_out,type=0,set_weater_back=None):
        self.set_weater = set_weater
        self.set_time = set_time[4:6] + '月' + set_time[-2:] + '日'
        self.end_time =  end_time[4:6] + '月' + end_time[-2:] + '日'
        self.code_name_set = code_name_set
        self.code_name_out = code_name_out
        self.text = ''
        self.type = type

        self.cut_time = (datetime.strptime(end_time, '%Y%m%d') - datetime.strptime(set_time, '%Y%m%d')).days + 1
        self.set_weater_back = set_weater_back
        self.original_data = original_data

    ######工具
    def fancheng(self):
        if self.type:
            if '雨' in self.set_weater_back['weater'].tolist()[0]:
                return 1
            else:
                return 0

    def rain(self):
        ##判断是否有雨，获取有雨的日期
        wat = self.original_data['weater'].tolist()
        rain = list(filter(lambda x:x if '雨' in x else 0, wat))

        return rain

    def weater_cut(self):
        ##日均温差
        self.original_data['wd_cut']= self.original_data['wd_max']- self.original_data['wd_min']
        self.original_data['wd_cut_gz'] = self.original_data.wd_cut.apply(lambda x: 1 if x >= 7 else 0)
        ##日期保存
        cut_weater_time = self.original_data[self.original_data['wd_cut_gz'] == 1]['YBTM'].apply(
            lambda x: x.strftime("%m-%d:")).tolist()  # %Y-%m-%d

        return cut_weater_time

    def weater_comparison(self):
        df2 = self.original_data.iloc[:-1, :].reset_index().iloc[:, 1:]  # 前一天的数据
        df1 = self.original_data.iloc[1:, :].reset_index().iloc[:, 1:]

        df2.columns = df2.columns.map(lambda x: x + '_1')
        original_data_wt = pd.concat([df1, df2], axis=1)
        original_data_wt['YBTM'] = [x[4:6] + '月' + x[-2:] + '日' for x in original_data_wt['YBTM'].tolist()]

        ##跟前一天的温度对比
        original_data_wt['flu_max'] = original_data_wt['wd_max'].astype(int) - original_data_wt['wd_max_1'].astype(int)
        original_data_wt['flu_min'] = original_data_wt['wd_min'].astype(int)  -original_data_wt['wd_min_1'].astype(int)

        ##最高温--白天的温度对比
        original_data_wt['original_low'] = original_data_wt.flu_max.apply(lambda x: 1 if x <= -3 else 0)
        original_data_wt['original_up'] = original_data_wt.flu_max.apply(lambda x: 1 if x >= 3  else 0)

        ##最高温--白天的温度对比
        original_data_wt['original_low_night'] =original_data_wt.flu_min.apply(lambda x: 1 if x <= -3 else 0)
        original_data_wt['original_up_night'] = original_data_wt.flu_min.apply(lambda x: 1 if x >= 3 else 0)
        return original_data_wt

    def specical_weater(self):
        ###特殊天气
        ts_weater = ['暴雨', '雷阵雨', '大雨', '雪']
        ts_dic = {}
        for indexs in self.original_data.index:
            weater =  self.original_data.ix[indexs,'weater']
            for ts in ts_weater:

                if ts in weater:
                    ts_time_pre = self.original_data.ix[indexs,'YBTM']
                    if ts not in ts_dic.keys():
                        pre = []
                        pre.append(ts_time_pre)
                        ts_dic[ts] = pre
                    else:
                        ts_dic[ts].append(ts_time_pre)

        return ts_dic
    ###########其他描述

    def weater_compare(self):

        ###两地天气对比
        ###白天天气对比 目的地-出发地
        max_weater_cut = self.original_data['wd_max'].apply(lambda x: int(x) - int(self.set_weater['wd_max'].tolist()[0])).tolist()

        # 减低的温度
        max_weater_cut_low = [abs(i) for i in max_weater_cut if i < -2]
        max_weater_cut_up = [abs(i) for i in max_weater_cut if i > 2]
        if max_weater_cut_low:
            wd_max = min(max_weater_cut_low)
            if len(max_weater_cut_low) == 1:
                wd_max = 0

            self.text += '白天温度比{}低{}~{}℃\n'.format(self.code_name_set,wd_max, max(max_weater_cut_low))
        elif max_weater_cut_up:
            wd_max = min(max_weater_cut_up)
            if len(max_weater_cut_low) == 1:
                wd_max = 0

            self.text += '白天温度比{}高{}~{}℃\n'.format(self.code_name_set,wd_max, max(max_weater_cut_up))
        else:
            self.text += '白天温度和{}相差不大\n'.format(self.code_name_set)

        ###晚上的温度
        min_weater_cut = self.original_data['wd_min'].apply(
            lambda x: int(x) - int(self.set_weater['wd_min'].tolist()[0])).tolist()

        min_weater_cut_low = [abs(i) for i in min_weater_cut if i <= -2]
        min_weater_cut_up = [abs(i) for i in min_weater_cut if i >= 2]
        if min_weater_cut_low:
            wd_low = min(min_weater_cut_low)
            if len(min_weater_cut_low) == 1:
                wd_low = 0
            self.text += '夜晚温度比{}低{}~{}℃\n'.format(self.code_name_set,wd_low, max(min_weater_cut_low))

        elif min_weater_cut_up:
            wd_low = min(min_weater_cut_up)
            if len(min_weater_cut_up) == 1:
                wd_low = 0

            self.text += '夜晚温度比{}高{}~{}℃\n'.format(self.code_name_set,wd_low, max(min_weater_cut_up))
        else:
            self.text += '夜晚温度和{}相差不大\n'.format(self.code_name_set)




    def out_weater(self):
        ############2、气象规则########################

        df2 = self.original_data.iloc[:-1, :].reset_index().iloc[:, 1:]  # 前一天的数据
        df1 = self.original_data.iloc[1:, :].reset_index().iloc[:, 1:]

        df2.columns = df2.columns.map(lambda x: x + '_1')
        original_data_wt = pd.concat([df1, df2], axis=1)
        original_data_wt['YBTM'] = [ x[4:6] + '月' + x[-2:]+'日' for x in original_data_wt['YBTM'].tolist()]
        rule_data = Expression(self.cut_time-1, original_data_wt)
        expression_day = rule_data.simple_day()
        self.text += expression_day